package com.qishanor.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qishanor.framework.constant.CacheConstant;
import com.qishanor.framework.util.R;
import com.qishanor.system.model.SysDict;
import com.qishanor.system.model.SysDictItem;
import com.qishanor.system.model.SysUser;
import com.qishanor.system.service.SysDictItemService;
import com.qishanor.system.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 字典管理
 * @author: 周振林
 * @date: 2022-04-12
 * @Copyright: 博客：http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@RestController
@RequestMapping("/api/sys/dict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private SysDictItemService sysDictItemService;

    /**
     * 通过字典类型查找字典
     * @param type 类型
     * @return 同类型字典
     */
    @GetMapping("/type/{type}")
    @Cacheable(value = CacheConstant.DICT_DETAIL, key = "#type", unless = "#result.data.isEmpty()")
    public R getDictByType(@PathVariable String type) {
        return R.ok(sysDictItemService.list(Wrappers.lambdaQuery(SysDictItem.class).eq(SysDictItem::getDictType, type)));
    }

    @GetMapping("/page")
    @SaCheckPermission("sys_dict_view")
    public R page(Page page, SysDict sysDict){

        return R.ok(sysDictService.page(page, Wrappers.lambdaQuery(sysDict)));
    }

    @PostMapping
    @SaCheckPermission("sys_dict_add")
    public R save(@RequestBody SysDict sysDict){
        return  R.ok(sysDictService.save(sysDict));
    }

    @PutMapping
    @SaCheckPermission("sys_dict_edit")
    @CacheEvict(value=CacheConstant.DICT_DETAIL,allEntries = true)
    public R updateById(@RequestBody SysDict sysDict){

        // 系统内置
        SysUser sysUser=StpUtil.getSession().getModel(CacheConstant.USER_DETAIL,SysUser.class);
        if (sysUser==null||sysUser.getIsSuper()!=1) {
            return R.failed("只有超级管理员才具有修改权限");
        }
        return R.ok(sysDictService.updateById(sysDict));
    }

    /**
     * 删除字典，并且清除字典缓存
     * @param id ID
     * @return R
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("sys_dict_del")
    public R removeById(@PathVariable String id) {
        return sysDictService.removeDict(id);
    }


    /**
     * 字典项相关
     */
    @GetMapping("/item/page")
    @SaCheckPermission("sys_dict_view")
    public R itemPage(Page page,SysDictItem sysDictItem){
        return R.ok(sysDictItemService.page(page, Wrappers.lambdaQuery(sysDictItem)));
    }

    @PostMapping("/item")
    @SaCheckPermission("sys_dict_add")
    @CacheEvict(value=CacheConstant.DICT_DETAIL,allEntries = true)
    public R saveItem(@RequestBody SysDictItem sysDictItem){
        return R.ok(sysDictItemService.save(sysDictItem));
    }
    @PutMapping("/item")
    @SaCheckPermission("sys_dict_edit")
    @CacheEvict(value=CacheConstant.DICT_DETAIL,allEntries = true)
    public R updateById(@RequestBody SysDictItem sysDictItem){
        SysUser sysUser= StpUtil.getSession().getModel(CacheConstant.USER_DETAIL,SysUser.class);
        if(sysUser==null||sysUser.getIsSuper()!=1){
            return R.failed("只有超级管理员才具有修改权限");
        }
        return R.ok(sysDictItemService.updateById(sysDictItem));
    }

    @DeleteMapping("/item/{id}")
    @SaCheckPermission("sys_dict_del")
    @CacheEvict(value = CacheConstant.DICT_DETAIL, allEntries = true)
    public R deleteById(@PathVariable String id){
        SysUser sysUser=StpUtil.getSession().getModel(CacheConstant.USER_DETAIL,SysUser.class);
        if(sysUser==null||sysUser.getIsSuper()!=1){
            return R.failed("只有超级管理员才具有删除权限");
        }
        return  R.ok(sysDictItemService.removeById(id));
    }

}
